<!doctype html>



  


<html class="theme-next pisces use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Android,MVVM," />





  <link rel="alternate" href="/atom.xml" title="GrantonZhuang's Blog" type="application/atom+xml" />




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.1" />






<meta name="description" content="0x00 修改gradle文件
整个项目的bulid.gradle

123456789buildscript &amp;#123;    repositories &amp;#123;        jcenter()    &amp;#125;    dependencies &amp;#123;        classpath &apos;com.android.tools.build:gradle:1.5.0&apos;        c">
<meta property="og:type" content="article">
<meta property="og:title" content="Android DataBinding 笔记">
<meta property="og:url" content="https://grantonzhuang.github.io/2016/01/22/Hello-Android-DataBinding/index.html">
<meta property="og:site_name" content="GrantonZhuang's Blog">
<meta property="og:description" content="0x00 修改gradle文件
整个项目的bulid.gradle

123456789buildscript &amp;#123;    repositories &amp;#123;        jcenter()    &amp;#125;    dependencies &amp;#123;        classpath &apos;com.android.tools.build:gradle:1.5.0&apos;        c">
<meta property="og:updated_time" content="2016-07-29T13:56:02.584Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Android DataBinding 笔记">
<meta name="twitter:description" content="0x00 修改gradle文件
整个项目的bulid.gradle

123456789buildscript &amp;#123;    repositories &amp;#123;        jcenter()    &amp;#125;    dependencies &amp;#123;        classpath &apos;com.android.tools.build:gradle:1.5.0&apos;        c">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: 0,
      author: '博主'
    }
  };
</script>




  <link rel="canonical" href="https://grantonzhuang.github.io/2016/01/22/Hello-Android-DataBinding/"/>

  <title> Android DataBinding 笔记 | GrantonZhuang's Blog </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">GrantonZhuang's Blog</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle"></p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/index.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/index.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/index.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Android DataBinding 笔记
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2016-01-22T21:55:35+08:00" content="2016-01-22">
              2016-01-22
            </time>
          </span>

          

          
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="0x00-修改gradle文件"><a href="#0x00-修改gradle文件" class="headerlink" title="0x00 修改gradle文件"></a>0x00 修改gradle文件</h1><ul>
<li>整个项目的<code>bulid.gradle</code></li>
</ul>
<figure class="highlight gradle"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">buildscript</span> &#123;</div><div class="line">    <span class="keyword">repositories</span> &#123;</div><div class="line">        jcenter()</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">dependencies</span> &#123;</div><div class="line">        <span class="keyword">classpath</span> <span class="string">'com.android.tools.build:gradle:1.5.0'</span></div><div class="line">        <span class="keyword">classpath</span> <span class="string">"com.android.databinding:dataBinder:1.+"</span></div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<a id="more"></a>
<ul>
<li>单个模块的<code>bulid.gradle</code></li>
</ul>
<figure class="highlight gradle"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">apply plugin: <span class="string">'com.android.application'</span></div><div class="line">apply plugin: <span class="string">'com.android.databinding'</span></div></pre></td></tr></table></figure>
<h1 id="0x01-格式"><a href="#0x01-格式" class="headerlink" title="0x01 格式"></a>0x01 格式</h1><p>在原有的布局文件中，加上一层<code>layout</code>标签包裹住，需要放置的数据，则放在<code>data</code>标签里。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">&lt;?xml version="1.0" encoding="utf-8"?&gt;</div><div class="line"><span class="tag">&lt;<span class="name">layout</span> <span class="attr">xmlns:android</span>=<span class="string">"http://schemas.android.com/apk/res/android"</span>&gt;</span></div><div class="line">   <span class="tag">&lt;<span class="name">data</span>&gt;</span></div><div class="line">       <span class="tag">&lt;<span class="name">variable</span> <span class="attr">name</span>=<span class="string">"field"</span> <span class="attr">type</span>=<span class="string">"com.example.type"</span>/&gt;</span></div><div class="line">   <span class="tag">&lt;/<span class="name">data</span>&gt;</span></div><div class="line">   <span class="comment">&lt;!-- here is the old layout--&gt;</span></div><div class="line">   <span class="tag">&lt;<span class="name">LinearLayout</span> /&gt;</span></div><div class="line">   <span class="comment">&lt;!-- here is the old layout--&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">layout</span>&gt;</span></div></pre></td></tr></table></figure>
<blockquote>
<p>note:<br>截止至<code>2016-04-01</code>，在目前的Android Studio IDE<code>1.5.1</code>以及dataBinder<code>1.0-rc5</code>版本中，还没有办法做到xml文件里，<code>data</code>标签里，包名，类名的<strong>自动补全</strong>。</p>
</blockquote>
<h1 id="0x02-数据绑定和事件绑定"><a href="#0x02-数据绑定和事件绑定" class="headerlink" title="0x02 数据绑定和事件绑定"></a>0x02 数据绑定和事件绑定</h1><ul>
<li><code>layout.xml</code>如下，对于<code>type</code>属性，如果该类属于<code>java.lang</code>包的话，可以省去包名，如<code>String</code>，<code>Integer</code>等。</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">&lt;?xml version="1.0" encoding="utf-8"?&gt;</div><div class="line"><span class="tag">&lt;<span class="name">layout</span> <span class="attr">xmlns:android</span>=<span class="string">"http://schemas.android.com/apk/res/android"</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">data</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">variable</span> <span class="attr">name</span>=<span class="string">"user"</span> <span class="attr">type</span>=<span class="string">"com.example.User"</span>/&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">variable</span> <span class="attr">name</span>=<span class="string">"listener"</span> <span class="attr">type</span>=<span class="string">"com.example.Listener"</span>/&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">data</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">LinearLayout</span></span></div><div class="line">        <span class="attr">android:layout_width</span>=<span class="string">"fill_parent"</span></div><div class="line">        <span class="attr">android:layout_height</span>=<span class="string">"fill_parent"</span>&gt;</div><div class="line">        <span class="tag">&lt;<span class="name">TextView</span></span></div><div class="line">            <span class="attr">android:layout_width</span>=<span class="string">"fill_parent"</span></div><div class="line">            <span class="attr">android:layout_height</span>=<span class="string">"fill_parent"</span></div><div class="line">            <span class="attr">android:text</span>=<span class="string">"@&#123;user.name&#125;"</span> /&gt;</div><div class="line">        <span class="tag">&lt;<span class="name">Button</span></span></div><div class="line">            <span class="attr">android:layout_width</span>=<span class="string">"fill_parent"</span></div><div class="line">            <span class="attr">android:layout_height</span>=<span class="string">"fill_parent"</span></div><div class="line">            <span class="attr">android:onClick</span>=<span class="string">"@&#123;listener.onClick&#125;"</span> /&gt;</div><div class="line">    <span class="tag">&lt;/<span class="name">LinearLayout</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">layout</span>&gt;</span></div></pre></td></tr></table></figure>
<ul>
<li><code>com.example.User.java</code>如下</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>&#123;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">final</span> String name;</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="title">User</span><span class="params">(String name)</span> </span>&#123;</div><div class="line">        <span class="keyword">this</span>.name = name;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li><code>com.example.Listener.java</code>如下</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Listener</span> </span>&#123;</div><div class="line">    <span class="comment">// 注意这里的onClick方法需要将View作为参数传入</span></div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">onClick</span><span class="params">(View view)</span></span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li><p>每一个使用dataBinding的layout文件，都会被编译产生一个对应的类，这个类的位置在<code>build/intermediates/classes/debug/packageName/databinding/xxxBinding.java</code>中，并且遵循驼峰命名法</p>
<blockquote>
<p>如 main_activity.xml –&gt; MainActivityBinding.java</p>
</blockquote>
</li>
<li><p>其中带有ID的控件，都会对应生成一个变量，同样遵循驼峰命名法</p>
<blockquote>
<p>如 btn_register –&gt; btnRegister</p>
</blockquote>
</li>
<li><p>基于<code>&lt;variable name=&quot;user&quot; type=&quot;com.example.User&quot;/&gt;</code>这样子的声明，生成的<code>xxxBinding.java</code>会有一个<code>setUser(User user)</code>方法。</p>
</li>
<li><p>在Activity里这样子调用和绑定</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Override</span></div><div class="line"><span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">(Bundle savedInstanceState)</span> </span>&#123;</div><div class="line">   <span class="keyword">super</span>.onCreate(savedInstanceState);</div><div class="line">   MainActivityBinding binding =</div><div class="line">             DataBindingUtil.setContentView(<span class="keyword">this</span>, R.layout.main_activity);</div><div class="line">   User user = <span class="keyword">new</span> User(<span class="string">"Test"</span>, <span class="string">"User"</span>);</div><div class="line">   binding.setUser(user);</div><div class="line">   binding.btnRegister.setText(<span class="string">"xxx"</span>);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li>在fragment中，可以在<code>onCreateView()</code>方法中进行数据绑定</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Override</span></div><div class="line"><span class="function"><span class="keyword">public</span> View <span class="title">onCreateView</span><span class="params">(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)</span> </span>&#123;</div><div class="line">    FragmentLoginBinding dataBinding =</div><div class="line">            DataBindingUtil.inflate(inflater, R.layout.fragment_login, container, <span class="keyword">false</span>);</div><div class="line">    <span class="comment">// something initial</span></div><div class="line">    <span class="keyword">return</span> dataBinding.getRoot();</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h1 id="0x03-BindAdapter的使用"><a href="#0x03-BindAdapter的使用" class="headerlink" title="0x03 BindAdapter的使用"></a>0x03 BindAdapter的使用</h1><p>对于非控件自带的attr，使用BindAdapter注解来实现，以一个<code>根据URL加载头像</code>的场景为例。</p>
<p>写一个ViewModel</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ImageViewModel</span> </span>&#123;</div><div class="line">    <span class="meta">@BindAdapter</span>(<span class="string">"bind:url"</span>)</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">void</span> <span class="title">loadAvatar</span><span class="params">(ImageView img, String avatarUrl)</span> </span>&#123;</div><div class="line">        <span class="comment">// load image by avatarUrl</span></div><div class="line">        loadAvatarByUrl(img, url);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>在布局文件中加入注解中绑定的url属性（需要加入appNS）</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">ImageView</span></span></div><div class="line">    <span class="attr">xmlns:app</span>=<span class="string">"http://schemas.android.com/apk/res-auto"</span></div><div class="line">    <span class="attr">android:layout_width</span>=<span class="string">"wrap_content"</span></div><div class="line">    <span class="attr">android:layout_height</span>=<span class="string">"wrap_content"</span></div><div class="line">    <span class="attr">app:url</span>=<span class="string">"@&#123;user.avatarUrl&#125;"</span></div><div class="line">    /&gt;</div></pre></td></tr></table></figure>
<p>再也不用在每一个需要用到头像的地方，都写类似下面的代码了。</p>
<p>虽然<code>loadAvatarByUrl(img, url)</code>的逻辑可能并不复杂，但是使用DataBinding胜在<code>将加载头像的逻辑完全从Activity或者Fragment中剥离出来</code>。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">ImageView img = (ImageView)findViewById(R.id.img);</div><div class="line">loadAvatarByUrl(img, url);</div></pre></td></tr></table></figure>
<h1 id="0x04-DataBinding在RecyclerView的使用"><a href="#0x04-DataBinding在RecyclerView的使用" class="headerlink" title="0x04 DataBinding在RecyclerView的使用"></a>0x04 DataBinding在RecyclerView的使用</h1><p>dataBinding对<code>RecyclerView</code>的支持也非常好，用法很简便。</p>
<p>首先在原来的item布局文件中，加入<code>&lt;layout&gt;</code>和<code>&lt;data&gt;</code>等标签。</p>
<p>接着，在ViewHolder中声明：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyViewHolder</span> <span class="keyword">extends</span> <span class="title">RecyclerView</span>.<span class="title">ViewHolder</span> </span>&#123;</div><div class="line"></div><div class="line">        <span class="keyword">private</span> MyItemBinding binding;</div><div class="line"></div><div class="line">        <span class="function"><span class="keyword">public</span> <span class="title">ExprHolder</span><span class="params">(View itemView)</span> </span>&#123;</div><div class="line">            <span class="keyword">super</span>(itemView);</div><div class="line">            <span class="comment">// 这样即可完成绑定</span></div><div class="line">            binding = DataBindingUtil.bind(itemView);</div><div class="line">        &#125;</div><div class="line"></div><div class="line">        <span class="function"><span class="keyword">public</span> MyItemBinding <span class="title">getBinding</span><span class="params">()</span> </span>&#123;</div><div class="line">            <span class="keyword">return</span> binding;</div><div class="line">        &#125;</div><div class="line">    &#125;</div></pre></td></tr></table></figure>
<p>在<code>onCreateViewHolder</code>回调方法中，将<code>LayoutInflater</code>加载进来的<code>View</code>作为构造函数传递给<code>ViewHolder</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Override</span></div><div class="line"><span class="function"><span class="keyword">public</span> MyViewHolder <span class="title">onCreateViewHolder</span><span class="params">(ViewGroup parent, <span class="keyword">int</span> viewType)</span> </span>&#123;</div><div class="line">    View view = LayoutInflater.from(parent.getContext())</div><div class="line">                   .inflate(R.layout.item, parent, <span class="keyword">false</span>);</div><div class="line">    <span class="keyword">return</span> <span class="keyword">new</span> MyViewHolder(view);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>在<code>onBindViewHolder</code>回调方法中，完成数据绑定</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Override</span></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onBindViewHolder</span><span class="params">(MyViewHolder holder, <span class="keyword">int</span> position)</span> </span>&#123;</div><div class="line">    Object obj = Objects.get(position);</div><div class="line">    holder.getBinding().setObj(obj); <span class="comment">// 完成数据绑定</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h1 id="0x05-其他用法"><a href="#0x05-其他用法" class="headerlink" title="0x05 其他用法"></a>0x05 其他用法</h1><ul>
<li><p>空值判断，使用<code>??</code>运算符进行空值判断，以下表达式的含义为，</p>
<ul>
<li>在TextView上，显示user的<code>name</code>字段，</li>
<li>user的<code>name</code>字段为空的话，显示user的<code>phone</code>字段，</li>
<li>如果user的<code>phone</code>字段为空的话，显示User的静态常量<code>NO_LOGIN</code>，作一个未登录的说明。</li>
</ul>
</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">TextView</span></span></div><div class="line">    <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></div><div class="line">    <span class="attr">android:layout_height</span>=<span class="string">"wrap_content"</span></div><div class="line">    <span class="attr">android:text</span>=<span class="string">"@&#123;user.name ?? user.phone ?? User.NO_LOGIN&#125;"</span> /&gt;</div></pre></td></tr></table></figure>
<p>原生代码对比：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">TextView tv = (TextView)findViewById(R.id.tv);</div><div class="line"><span class="keyword">if</span> (<span class="string">""</span>.equals(user.getName())) &#123;</div><div class="line">    <span class="keyword">if</span> (<span class="string">""</span>.equals(user.getPhone())) &#123;</div><div class="line">        tv.setText(User.NO_LOGIN);</div><div class="line">        <span class="keyword">return</span>;</div><div class="line">    &#125;</div><div class="line">    tv.setText(user.getPhone());</div><div class="line">    <span class="keyword">return</span>;</div><div class="line">&#125;</div><div class="line">tv.setText(user.getName());</div></pre></td></tr></table></figure>
<ul>
<li>使用<code>import</code>标签引用一个包含有静态方法的辅助类</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">data</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">variable</span> <span class="attr">name</span> = <span class="string">"mInteger"</span> <span class="attr">type</span> = <span class="string">"int"</span>/&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">import</span> <span class="attr">type</span> = <span class="string">"com.xxx.XxxHelper"</span> /&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">data</span>&gt;</span></div><div class="line"></div><div class="line"><span class="comment">&lt;!-- 在布局中可以这样子调用，就可以在java代码里省下findViewById，在if else中setVisibility()...了 --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">TextView</span></span></div><div class="line">    <span class="attr">android:layout_width</span>=<span class="string">"fill_parent"</span></div><div class="line">    <span class="attr">android:layout_height</span>=<span class="string">"fill_parent"</span></div><div class="line">    <span class="attr">android:visibility</span>=<span class="string">"@&#123;XxxHelper.hasPermisson(mInteger) ? View.VISIBLE:View.GONE&#125;"</span>/&gt;</div></pre></td></tr></table></figure>
<ul>
<li>使用资源文件</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">data</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">variable</span> <span class="attr">name</span>=<span class="string">"mode"</span> <span class="attr">type</span>=<span class="string">"int"</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">import</span> <span class="attr">type</span>=<span class="string">"com.config.Constants"</span> /&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">data</span>&gt;</span></div><div class="line"><span class="comment">&lt;!-- 在布局文件中引用资源文件 --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">Button</span></span></div><div class="line">    <span class="attr">android:id</span>=<span class="string">"@+id/btn_register"</span></div><div class="line">    <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></div><div class="line">    <span class="attr">android:layout_height</span>=<span class="string">"wrap_content"</span></div><div class="line">    <span class="attr">android:text</span>=<span class="string">"@&#123;mode == Constants.MODE_REGISTER? @string/btn_register : @string/reset_password&#125;"</span> /&gt;</div></pre></td></tr></table></figure>
<p><code>com.config.Constants</code>这样子写<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Constants</span> </span>&#123;</div><div class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> MODE_REGISTER = <span class="number">0</span>;</div><div class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> MODE_RESET_PASSWORD = <span class="number">1</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<ul>
<li>别名 如果<code>import</code>标签引用到的两个类名相同，可以使用<code>alias</code>属性</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">data</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">import</span> <span class="attr">type</span>=<span class="string">"com.package.one.Klass"</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">import</span> <span class="attr">type</span>=<span class="string">"com.package.two.Klass"</span> <span class="attr">alias</span>=<span class="string">"KlassTwo"</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">data</span>&gt;</span></div></pre></td></tr></table></figure>
<h1 id="0x06-开发过程中遇到的坑"><a href="#0x06-开发过程中遇到的坑" class="headerlink" title="0x06 开发过程中遇到的坑"></a>0x06 开发过程中遇到的坑</h1><ol>
<li><p>在写xml文件的时候，由于插件的支持有限，有关dataBinding这一部分没有自动补全和提示，有时候编译的时候报错说<code>xxxDataBinding类不存在</code>之类的信息，一定是手抖写错了表达式，只要将Android Studio的<code>message</code>窗口（就是那个查看编译反馈的窗口）拉到最下面，一般都可以看到是哪一个布局文件中表达式写错了，修改对应的表达式即可。</p>
</li>
<li><p>由于dataBinding插件是实时编译的，有时候会出现以下情况，修改了某个布局文件的名字，或者其中控件的ID，也会报错，这时候只要找到对应生成的java文件删除，让插件自动重新编译即可。对应的java文件位置在：<code>build/intermediates/classes/debug/packageName/databinding/xxxBinding.java</code>，或者删除整个<code>build</code>文件夹也可。</p>
</li>
</ol>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Android/" rel="tag">#Android</a>
          
            <a href="/tags/MVVM/" rel="tag">#MVVM</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/01/02/HelloHexo/" rel="next" title="用Hexo搭建个人博客">
                <i class="fa fa-chevron-left"></i> 用Hexo搭建个人博客
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/04/19/动态代理与Retrofit/" rel="prev" title="动态代理与Retrofit">
                动态代理与Retrofit <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="http://ww1.sinaimg.cn/mw690/6e8939a6gw1f3g1u1mc6lj20hs0hsmxf.jpg"
               alt="Granton Zhuang" />
          <p class="site-author-name" itemprop="name">Granton Zhuang</p>
          <p class="site-description motion-element" itemprop="description"></p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives/index.html">
              <span class="site-state-item-count">26</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">11</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        
          <div class="feed-link motion-element">
            <a href="/atom.xml" rel="alternate">
              <i class="fa fa-rss"></i>
              RSS
            </a>
          </div>
        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#0x00-修改gradle文件"><span class="nav-number">1.</span> <span class="nav-text">0x00 修改gradle文件</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#0x01-格式"><span class="nav-number">2.</span> <span class="nav-text">0x01 格式</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#0x02-数据绑定和事件绑定"><span class="nav-number">3.</span> <span class="nav-text">0x02 数据绑定和事件绑定</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#0x03-BindAdapter的使用"><span class="nav-number">4.</span> <span class="nav-text">0x03 BindAdapter的使用</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#0x04-DataBinding在RecyclerView的使用"><span class="nav-number">5.</span> <span class="nav-text">0x04 DataBinding在RecyclerView的使用</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#0x05-其他用法"><span class="nav-number">6.</span> <span class="nav-text">0x05 其他用法</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#0x06-开发过程中遇到的坑"><span class="nav-number">7.</span> <span class="nav-text">0x06 开发过程中遇到的坑</span></a></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Granton Zhuang</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>

        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.1"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.0.1"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.1"></script>



  



  




  
  

  

  

  

</body>
</html>
